package com.house.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: GGP
 * @description: 拦截器
 * @time: 2023-03-08 10:46
 **/
public class DomainInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
			//	设置响应头 //表示接收的域名的请求（*表示接收任意，获取请求时的Origin)
			response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
			response.setHeader("Access-Control-Allow-Credentials", "true");
			response.setHeader("P3P", "CP=CAO PSA OUR");
			if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
				response.addHeader("Access-Control-Allow-Methods",request.getHeader("Access-Control-Request-Method"));
				response.addHeader("Access-Control-Allow-Headers",request.getHeader("Access-Control-Request-Headers"));
				response.addHeader("Access-Control-Max-Age", "120");
			}
			if(!(handler instanceof HandlerMethod)){
				return false;
			}
			response.setContentType("application/json;charset=utf-8");
		return true;
	}
}
